//
//  MyTenderController.swift
//  SlaughterClient
//
//  Created by 邱仙凯 on 2020/1/2.
//  Copyright © 2020 zwb. All rights reserved.
//

import UIKit

class ZyaMyTenderVC: ZyaTenderBaseTableVC {
    
    @IBOutlet weak var rightLab: UILabel!
    @IBOutlet weak var leftBtnView: UIView!
    @IBOutlet weak var rightBtnView: UIView!
    @IBOutlet weak var name: UILabel!
    var dataArray =  [ZyaZtbContentModel]()
 
     override func viewDidLoad() {
        super.viewDidLoad()
         self.title = "我的"
        name.text = USERINFO.kfName
        tableView.mj_header = KSRefreshHeader {
            self.network()
        }
        tableView.mj_header?.beginRefreshing()
        tableView.register(XSBaojiaCell.self, forCellReuseIdentifier: XSBaojiaCell.identifier)
 
        tableView.tableFooterView = UIView()
        
        
        let tapGesture1 = UITapGestureRecognizer(target: self, action: #selector(leftGo))
        leftBtnView.addGestureRecognizer(tapGesture1)
        let tapGesture2 = UITapGestureRecognizer(target: self, action: #selector(rightGo))
        rightBtnView.addGestureRecognizer(tapGesture2)
        rightLab.text =  "关注的采购信息"
         if #available(iOS 15.0, *) {
                     tableView.sectionHeaderTopPadding = 0
                 }
    }
    @objc func leftGo() {
        
        let view =  ZyaDepositMgtVC()
        self.navigationController?.pushViewController(view, animated: true)
        
    }
    @objc func rightGo() {
         let  vc = ZBMyMarkVC()
        self.navigationController?.pushViewController(vc, animated: true)
        
    }
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 60
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return self.dataArray.count
        
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        
        var one = self.dataArray[indexPath.row]
        guard let cell = tableView.dequeueReusableCell(withIdentifier: XSBaojiaCell.identifier, for: indexPath) as? XSBaojiaCell else {
            fatalError("cell error")
        }
        cell.makeConstrainsts()
        one.listType = .TB
        one.innerType = ZTBInnerType.tbMytbKHW
        cell.btnDtradeType.isHidden = false

        cell.one = one
        
       
            return cell
         
        
        
    }
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        var idkey = ""
        
            idkey = self.dataArray[indexPath.row].idKey.unwrap.isEmpty ? self.dataArray[indexPath.row].zZbId.unwrap : self.dataArray[indexPath.row].idKey.unwrap
        
        
        var inType:ZTBInnerType!
        
            inType = .tbMytbKHW
        
        
        zyago2TenderInfoVC(listType: .TB, fromVC: self, idkey: idkey, innerType: inType)
        
    }
    
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        let headerView = UIView(frame: CGRect(x: 0, y: 0, width: KSGlobal.screenW, height: 30))
        headerView.backgroundColor = .white
        
        let dark = UIView(frame: CGRect(x: 0, y: 0, width: KSGlobal.screenW, height: 10))
        dark.backgroundColor = #colorLiteral(red: 0.9333333333, green: 0.9333333333, blue: 0.9333333333, alpha: 1)
        headerView.addSubview(dark)
        
        let title = UILabel(frame: CGRect(x: 10, y: 18, width: 100, height: 30))
        title.font = UIFont.systemFont(ofSize: 15)
        
            title.text = "我的报价"
       
        
        let btn = UIButton(frame: CGRect(x: KSGlobal.screenW - 100, y: 18, width: 90, height: 30))
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        btn.tag = section
        btn.setTitleColor(.darkGray, for: .normal)
        btn.addTarget(self, action: #selector(viewAll(sender:)), for: .touchUpInside)
        btn.setTitle("查看全部  >", for: .normal)
        headerView.addSubview(btn)
        headerView.addSubview(title)
        return headerView
    }
    
    @objc func viewAll(sender:UIButton) {
     
        let  vc = ZBMyChujiaVC()
            self.navigationController?.pushViewController(vc, animated: true)
       
        
        
        
    }
    func network () {
        
            
            KSNetwork.shared.doRequest(API.tender(.queryMytenderZbBidList(z_begin_dt: "", z_end_dt: "", z_province: "",z_city: "", z_price_min: "", z_price_max: "", page: "1", size: "3", z_title: "", category: "")),showHud: false) { (data:BaseResult<ZyaZtbContentModel>) in
                if data.flag == TRUE {
                    if let infos = data.infos {
                        
                        self.dataArray = infos
                        
                        
                    }
                    
                    
                } else {
                    
                }
                self.tableView.reloadData()
                self.tableView.mj_header?.endRefreshing()
                
            }
 
        
    }
    
    
    
}
